package com.qcby.chatservice.web.config;


import com.qcby.chatservice.common.result.Result;
import com.qcby.chatservice.common.enums.ResponseStatus;
import com.qcby.chatservice.common.exception.ChatException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 聊天服务异常拦截
 *
 * @author cong.zhen
 * @date 2023/01/08
 */

@RestControllerAdvice
@Slf4j
public class ChatServiceExceptionAdvice {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        log.error("执行异常", e);
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
            return Result.getBusinessException(exception.getMessage(), ResponseStatus.BUSINESS_ERROR_STATUS.getCode());
        } else if (e instanceof ChatException) {
            ChatException exception = (ChatException) e;
            return Result.getBusinessException(exception.getMessage(), ResponseStatus.BUSINESS_ERROR_STATUS.getCode());
        } else {
            return Result.getBusinessException("后台异常，请联系管理员！", ResponseStatus.BUSINESS_ERROR_STATUS.getCode());
        }
    }
}
